package gemini.castor.ui.client.page.widget.categorylist;

import gemini.basic.model.ProductType;
import gemini.castor.ui.client.page.widget.categorypreview.CategoryPreviewEvent;
import gemini.castor.ui.client.page.widget.categorypreview.CategoryPreviewHandler;
import gemini.castor.ui.client.page.widget.categorypreview.CategoryPreviewWidget;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Panel;

public class CategoryListWidget extends Composite {

	@UiTemplate("CategoryListWidget.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, CategoryListWidget> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

	public CategoryListWidget() {
		initWidget(binder.createAndBindUi(this));
	}

	@UiField
	HorizontalPanel content;
	
	public void loadData(List<ProductType> productTypes){
		content.clear();
		for (final ProductType proType : productTypes) {
			CategoryPreviewWidget prPreview = new CategoryPreviewWidget();
			prPreview.loadData(proType);
			prPreview.addCategoryPreviewHandler(new CategoryPreviewHandler() {
				
				@Override
				public void onSelected(CategoryPreviewEvent productPreviewEvent) {
					CategoryListEvent event = new CategoryListEvent(
							CategoryListEvent.ForwardType.SELECT_CATEGORY);
					event.setSelectedCategory(proType);
					fireEvent(event);
				}
			});
			
			content.add(prPreview);
		}
	}
	
	public HandlerRegistration addCategoryListHandler(CategoryListHandler handler){
		return this.addHandler(handler, CategoryListEvent.getType());
	}
}
